package cn.pegasus.teamwork.interfaces.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.iyizhan.teamwork.base.util.ObjectUtil;
import cn.pegasus.teamwork.interfaces.entity.BussInterfaceParam;
import cn.pegasus.teamwork.interfaces.mapper.BussInterfaceParamMapper;
import cn.pegasus.teamwork.interfaces.service.BussInterfaceParamService;
import cn.pegasus.teamwork.manager.controller.BaseManagerController;
@Controller
@RequestMapping("/InterfaceParam/*")
public class BussInterfaceParamController extends BaseManagerController<BussInterfaceParam, BussInterfaceParamMapper> {
    Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private BussInterfaceParamService bussInterfaceParamservice;
    @Autowired
    public BussInterfaceParamController(BussInterfaceParamService bussInterfaceParamservice){
    	setBaseService(bussInterfaceParamservice);
    }
    /**
     * 
     * @Description: TODO 接口参数查询
     * @param map
     * @return	   
     * @throws
     * @author zhoulibo
     * @date 2015年11月16日
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public Map<String, Object> query(@RequestParam Map<String, Object> map) {
        boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "interfaceId", "paramType" }, map);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        if (isNotNull) {
            List<BussInterfaceParam> list = bussInterfaceParamservice.selectParam(map);
            resultMap.put("list", list);
        }else{
        	
        }
        return resultMap;

    }
    /**
     * 
     * @Description: TODO 添加接口参数数据提交
     * @param map
     * @return	   
     * @throws
     * @author zhoulibo
     * @date 2015年11月16日
     */
    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Map<String, Object> add(@RequestParam Map<String, Object> map) {
        Map<String, Object> resultMap = null;
        try {
            resultMap = bussInterfaceParamservice.addParam(map,getAdmin().getId());
        } catch (Exception e) {
        	logger.error("添加接口参数失败",e);
        }

        return resultMap;

    }
    /**
     * @Description: TODO  修改接口参数提交
     * @param map
     * @return	   
     * @throws
     * @author zhoulibo
     * @date 2015年11月16日
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Map<String, Object> update(@RequestParam Map<String, Object> map) {
        boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "id"}, map);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            if (isNotNull) {
                resultMap = bussInterfaceParamservice.updateParam(map,getAdmin().getId());
            }
        } catch (Exception e) {
          logger.error("修改接口参数失败", e);
        }

        return resultMap;

    }

    /**
     * @Description: TODO 接口参数删除
     * @param map
     * @return	   
     * @throws
     * @author zhoulibo
     * @date 2015年11月16日
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Map<String, Object> delete(@RequestParam Map<String, Object> map) {
        boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "id"}, map);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            if (isNotNull) {
                resultMap = bussInterfaceParamservice.deleteParam(map,getAdmin().getId());
            }
        } catch (Exception e) {
           logger.error("删除接口参数错误",e);
        }

        return resultMap;

    }
    
	/**
	 * @Description: TODO 接口参数修改或者新增
	 * @param map
	 * @return
	 * @throws
	 * @author zhoulibo
	 * @date 2015年11月16日
	 */
	@RequestMapping(value = "/jumpEditOrAdd", method = RequestMethod.GET)
	public ModelAndView jumpEditOrAdd(@RequestParam Map<String, Object> map) {
		boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "id" },
				map);
		ModelAndView mv = new ModelAndView("interfaces/paramEdit");
		BussInterfaceParam param = null;
		try {
			// 修改
			if (isNotNull) {
				param = bussInterfaceParamservice.detail(map);
				// 新增
			} else {
				param = new BussInterfaceParam();
				// 接口信息id
				param.setInterfaceId(Integer.valueOf(map.get("interfaceId").toString()));
				// 参数类型 输入 输出
				param.setParamType(Integer.valueOf(map.get("paramType").toString()));
			}
			mv.addObject("param", param);
		} catch (Exception e) {
			logger.error("修改接口参数/新增失败",e);
		}
		return mv;
	}
}
